<?php
class Situation extends CI_Model
{
	function createSituation($situation)
	{
		/*for($i=0 ; $i<$situation.length() ; $i++) // checking that the array is not empty
		{
			
			if($situation[$i]==null )
			{
				return false;
				
			}
		}*/
		//$situation['modificationDate']='CURTIME()';
		$insert= $this->db->insert('situation',$situation); //inserting the array in the database
		return $insert;
		
	}

	function deleteSituation ($sid) //modified by salem
	{
		/*if($sid==null)
		{
			return false;
		}
		else 
		{*/
				//$delete= $this ->db -> delete('situation',$sid);
				
				
				$this->db->where('id', $sid);
				$this->db->delete('situation');
				
				
				//return $this;
		//}
		
	}
	
	function editSituation($sid,$situation)
	{		
		/*$data = array (
						'description'=> $description,
						'name' => $namee,
						'privacyType'=> $privacyType,
					   );*/
                //$situation['modificationDate']='CURTIME()';
		$this->db->where('id', $sid);
                //$update = $this->db->query("");
		$update= $this->db->update('situation',$situation ); //the third parameter is for WHERE claus
		return $update;
	}
	
	function searchSituation($keyword)
	{
		if($keyword==NULL)
		{
			//echo 'Empty Keyword...Good Bye';
		}
		else 
		{
			//SELECT part is omitted because it is SELECT *
			$this->db->from('situation');
			$this->db->where('name',$keyword);
			$this->db->where('privacyType','public'); //privacy type is not boolean, it is varchar!!
			
			$quere=$this->db->get();// this will get the resutl
			
			return $quere;
		}
	}
	
	function searchMySituation($sid,$keyword)
	{
		if ($keyword==NULL)
		{
		return false;
		}
		else 
		{	
			//SELECT part is omitted because its SELECT *
			$this->db->from('situation');
			$this->db->where('id',$sid);
			
			$quere=$this->db->get();
			
			return $quere;
		}
	}
	
	//The following function is done by Basim
	function changePersonalInfo($email, $dateOfbirth, $firstName, $lastName, $password)
	{
		/*Notes: - when the user wants to change his password he should be asked to enter a password confirmation
		and it should be validated at the client side that they are matched
		- we should provide one more function that handles the scenario when the user forget his password*/
		
		$data = array (
						'email'=> $email,
						'dateOfbirth' => $dateOfBirth, //till now I'm not convinced of the benefit of dateOfBirth!!
						'firstName'=> $firstName,
						'lastName'=> $lastName,
						'password'=> $password
					   );
		
		$update= $this->db->update('userProfile',$email,$dateOfbirth, $password); // what is that ??? this is wrong ... you should pass the $data as the second parameter...or what is the 	
																				  //binifit of creating an array then ????  "Mr.Hell"
		return $update;
	}
	function getMySituations($user_id)
	{
		if ($user_id==NULL)
		{
		return false;
		}
		else 
		{	
			//SELECT part is omitted because its SELECT *
			$this->db->select('id,name,html,privacyType,description,context_count');
			$this->db->from('situation');
			$this->db->where('UserProfile_email',$user_id);
                        //$this->db->order_by("id", "desc");
                        $this->db->order_by("modificationDate", "desc");
			
			$quere=$this->db->get();
			
			return $quere;
		}
	}
	function getMySituation_html($sid)
	{
		if ($sid==NULL)
		{
		return false;
		}
		else 
		{	
			//SELECT part is omitted because its SELECT *
			$this->db->select('id,name,html,privacyType,description,context_count,background_image');
			$this->db->from('situation');
			$this->db->where('id',$sid);
			
			$quere=$this->db->get();
			
			return $quere;
		}
	}
	//mousa and hussam
        function addFriend ($sit_id,$friendEmail)
        {
            //The function will return
            // 0 if the user is not registerd
            // 1 if the user is not added in the situation and add him
            // 2 if the user is alreadu added in the situation

            //first checking if the user is registerd
            $this->db->select('email');
            $this->db->from('userprofile');
            $this->db->where('email',$friendEmail);
            //getting the resutls
            $query=$this->db->get();
            $rows_num=$query->num_rows;

            if($rows_num == 0)
            {
                return '0';
            }

            //checking if the user is not added in the situation
            //SELECT is ommitted becase its SELECT *
            $this->db->from('situationfriendlist');
            $this->db->where('SituationID',$sit_id);
            $this->db->where('friendEmail',$friendEmail);
            //getting the results
            $query=$this->db->get();
            $rows_num=$query->num_rows;

            if($rows_num ==0)
            {
                // put the data in an array for inserting
                $data = array (
                    'friendEmail' => $friendEmail,
                    'SituationID' => $sit_id
                );

                $this->db->insert('situationfriendlist',$data);
                return '1';
            }

            else
            {
                return '2'; // the user is already a friend
            }

        }

        function deleteFriend ($sit_id,$friendEmail)
        {

            $this->db->where('SituationID',$sit_id);
            $this->db->where('friendEmail',$friendEmail);
            $this->db->delete('situationfriendlist');
        }

        function getSitInfo($sit_id)
        {
            // this function is needed for getting the information of the sit
            // to use it in sending the mail to a friend when adding him

            $this->db->select('name');
            $this->db->from('situation');
            $this->db->where('id',$sit_id);
            $query=$this->db->get();

            return $query;


        }

        function getSitOwnerInfo ($sit_id)
        {
            // this function is needed for getting the information of the sit owner
            // to use it in sending the mail to a friend when adding him

            //getting the user email
            $this->db->select('UserProfile_email');
            $this->db->from('situation');
            $this->db->where('id',$sit_id);
            $query=$this->db->get();

            foreach($query->result() as $row)
            {
                $email= $row->UserProfile_email;
            }



            //Getting the information of the owner
            $this->db->select('firstName');
            $this->db->select('lastName');
            $this->db->select('email');
            $this->db->from('userprofile');
            $this->db->where('email',$email);
            $query=$this->db->get();

            return $query;


        }
          function getMySituationsAll($user_id)
	{
		if ($user_id==NULL)
		{
		return false;
		}
		else
		{
			//SELECT part is omitted because its SELECT *
			$this->db->select('id,name,html,privacyType,description,context_count');
			$this->db->from('situation');
			$this->db->where('UserProfile_email',$user_id);
			$this->db->order_by('name','ASC');
			$quere=$this->db->get();

			return $quere;
		}
	}
        function getAllSituationsInfo($user_id)
	{
		if ($user_id==NULL)
		{
		return false;
		}
		else
		{
			$this->db->select('id,name,privacyType,description');
			$this->db->from('situation');
			$this->db->where('UserProfile_email',$user_id);
			$this->db->order_by('name','ASC');
			$quere=$this->db->get();

			return $quere;
		}
	}

        function getSuggestion($sit_name)
        {
          //  $this->db->select('id');
                    $this->db->from('situation');
                    $this->db->where('name',$sit_name);
                    $this->db->where('privacyType','public');
                   // $this->db->or_where('privacyType','Friends only');
                    $query=$this->db->get();

                     return $query;

        }


   

}